import utils from '@/mixins/utils.js';
export default {
  name: 'editInterCome',
  mixins: [utils],
  data() {
    return {
      tabData: ['呼叫中心', '通道对讲机'],
      currentTab: '呼叫中心',
      list: null,
      selectBtnStyleObject: {},
      dialogSetting: {
        addInterComeAccountDialogVisiable: false,
        addInterComeAccountChannelDialogVisiable: false
      },
      searchVal: '',
      channelNameSearchVal: '',
      createIntercomeAccount: {
        groupComboList: [],
        webPhoneChannelList: [],
        channelIds: [],
        originCheckedChannelIds: [],
        defaultProps: {
          children: 'operatorChannelPhoneDataVoList',
          label: 'name',
          disabled: 'hasPhoneFlag'
        },
        isAdd: false,
        parkCloudWebPhoneGroupAddReq: {
          extCount: 1,
          parkCloudWebPhoneGroupVo: {
            groupName: '',
            id: ''
          }
        }
      }
    };
  },
  created() {
    this.getOperatorWebPhoneView();
  },
  methods: {
    back() {
      this.$router.push({
        name: 'manageIndex'
      });
    },
    interComeSelectChange(val) {},
    getOperatorWebPhoneView() {
      API.fetchPost('parkWebPhone/getOperatorWebPhoneView').then(
        res => {
          if (!this.list) {
            res.data.parkCloudWebPhoneGroupExtVoList.forEach(item => {
              item.isShow = true;
            });
            this.list = res.data;
          } else {
            const parkCloudWebPhoneGroupExtVoList = this.list.parkCloudWebPhoneGroupExtVoList;
            res.data.parkCloudWebPhoneGroupExtVoList.forEach(item => {
              const curItem = parkCloudWebPhoneGroupExtVoList.find(
                citem => citem.parkCloudWebPhoneGroupVo.id === item.parkCloudWebPhoneGroupVo.id
              );
              if (curItem) {
                item.isShow = curItem.isShow;
              } else {
                item.isShow = true;
              }
            });

            this.list = res.data;
          }
        },
        err => {}
      );
    },
    async addInterComeAccount() {
      await this.getOperatorGroupComboList();
      this.createIntercomeAccount.parkCloudWebPhoneGroupAddReq.extCount = 1;
      this.createIntercomeAccount.parkCloudWebPhoneGroupAddReq.parkCloudWebPhoneGroupVo.groupName = '';
      this.dialogSetting.addInterComeAccountDialogVisiable = true;
    },
    async addInterComeChannelAccount() {
      await this.getOperatorWebPhoneChannelListDataView();
      this.createIntercomeAccount.channelIDList = [];
      this.dialogSetting.addInterComeAccountChannelDialogVisiable = true;
    },
    getOperatorGroupComboList() {
      return API.fetchGet('/parkWebPhone/group/getOperatorGroupComboList').then(
        res => {
          this.createIntercomeAccount.groupComboList = res.data;
          if (!res.data.length) {
            this.createIntercomeAccount.isAdd = true;
          } else {
            this.createIntercomeAccount.isAdd = false;
            this.createIntercomeAccount.parkCloudWebPhoneGroupAddReq.parkCloudWebPhoneGroupVo.id = res.data[0].id;
          }
        },
        err => {}
      );
    },
    getOperatorWebPhoneChannelListDataView() {
      return API.fetchPost('parkWebPhone/getOperatorWebPhoneChannelListDataView').then(
        res => {
          this.createIntercomeAccount.webPhoneChannelList = res.data.map(item => {
            item.name = item.parkName;
            return item;
          });
          let checkedChannelIds = [];
          this.createIntercomeAccount.channelIds = res.data.forEach(item => {
            item.operatorChannelPhoneDataVoList.forEach(item => {
              if (item.hasPhoneFlag) {
                checkedChannelIds.push(item.id);
              }
            });
          });
          this.createIntercomeAccount.originCheckedChannelIds = JSON.parse(JSON.stringify(checkedChannelIds));
          this.createIntercomeAccount.channelIds = checkedChannelIds;
        },
        err => {}
      );
    },
    submitAddInterComeAccount() {
      let params = JSON.parse(JSON.stringify(this.createIntercomeAccount.parkCloudWebPhoneGroupAddReq));
      if (this.createIntercomeAccount.isAdd) {
        params.parkCloudWebPhoneGroupVo.id = '';
      } else {
        params.parkCloudWebPhoneGroupVo.groupName = this.createIntercomeAccount.groupComboList.find(
          item => item.id === params.parkCloudWebPhoneGroupVo.id
        ).name;
      }
      this.dialogSetting.addInterComeAccountDialogVisiable = false;
      API.fetchPost('parkWebPhone/group/saveWebPhoneForOperatorGroupAndExtension', params).then(
        res => {
          this.showToast('操作成功！', 1, 1500);
          this.getOperatorWebPhoneView();
        },
        err => {}
      );
    },
    submitAddInterComeChannelAccount() {
      let channelIds = this.$refs.channelTree.getCheckedKeys().filter(item => item !== undefined);
      channelIds = channelIds.filter(item => !this.createIntercomeAccount.originCheckedChannelIds.includes(item));
      if (!channelIds.length) {
        this.showToast(`请至少选择一个通道`, 2, 2000);
        return;
      }
      this.dialogSetting.addInterComeAccountChannelDialogVisiable = false;
      API.fetchPost('/parkWebPhone/channel/addBatchWebPhoneForChannelList', channelIds).then(
        res => {
          this.showToast('操作成功！', 1, 1500);
          this.getOperatorWebPhoneView();
        },
        err => {}
      );
    },
    delGroup(item) {
      this.$confirm(`确定删除${item.groupName}及其分机的账号？`, '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'mgr-msg-box',
        confirmButtonClass: 'el-button el-button--danger'
      }).then(() => {
        API.fetchPost('/parkWebPhone/group/delWebPhoneForOperatorGroup', item).then(
          res => {
            this.showToast('操作成功！', 1, 1500);
            this.getOperatorWebPhoneView();
          },
          err => {}
        );
      });
    },
    delGroupItem(item) {
      this.$confirm(`确定删除分机号码:${item.phoneNumber}？`, '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'mgr-msg-box',
        confirmButtonClass: 'el-button el-button--danger'
      }).then(() => {
        API.fetchPost('/parkWebPhone/group/delWebPhoneForOperatorGroupExtension', item).then(
          res => {
            this.showToast('操作成功！', 1, 1500);
            this.getOperatorWebPhoneView();
          },
          err => {}
        );
      });
    },
    delChannel(item) {
      this.$confirm(`确定删除${item.parkCloudWebPhoneVo.mappingName}的语音对讲账号？`, '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        customClass: 'mgr-msg-box',
        confirmButtonClass: 'el-button el-button--danger'
      }).then(() => {
        API.fetchPost('/parkWebPhone/channel/deleteWebPhoneForChannel', {
          id: item.parkCloudWebPhoneVo.mappingId
        }).then(
          res => {
            this.showToast('操作成功！', 1, 1500);
            this.getOperatorWebPhoneView();
          },
          err => {}
        );
      });
    }
  },
  computed: {
    filterSearchChannelList() {
      if (this.searchVal && this.list.parkCloudWebPhoneParkChannelVoList.length) {
        return this.list.parkCloudWebPhoneParkChannelVoList.filter(item => item.parkName.includes(this.searchVal));
      } else {
        return [];
      }
    },
    channelNameSearchList() {
      if (this.channelNameSearchVal) {
        if (this.filterSearchChannelList.length) {
          const list = JSON.parse(JSON.stringify(this.filterSearchChannelList));
          return list.filter(item => {
            return item.parkCloudWebPhoneVo.mappingName.includes(this.channelNameSearchVal);
          });
        } else {
          return this.list.parkCloudWebPhoneParkChannelVoList.filter(item => {
            return item.parkCloudWebPhoneVo.mappingName.includes(this.channelNameSearchVal);
          });
        }
      } else {
        return [];
      }
    },
    tableList() {
      if (this.channelNameSearchList.length) {
        return this.channelNameSearchList;
      } else if (this.filterSearchChannelList.length) {
        return this.filterSearchChannelList;
      } else if (this.list && this.list.parkCloudWebPhoneParkChannelVoList) {
        return this.list.parkCloudWebPhoneParkChannelVoList;
      } else {
        return [];
      }
    }
  }
};
